<template>
  <div>
    <!-- 信息修改 -->
    <el-dialog v-model="infoEditVisible" title="修改用工信息" width="800px">
      <el-form ref="infoEditFormRef" :model="infoEditForm" :rules="rules" label-width="auto">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item
              label="手机号"
              prop="phone"
              required
              :rules="[
                { required: true, pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号', trigger: 'blur' }
            ]"
            >
              <el-input v-model="infoEditForm.phone" placeholder="请输入手机号" clearable />
            </el-form-item>
          </el-col>

          <template v-if="checkRole(5,7)">
            <el-col :span="12">
              <el-form-item label="身份证正面：" prop="cardPositive">
                <imageUpload v-if="infoEditForm.cardPositive" v-model="infoEditForm.cardPositive" modelKey="url" :limit="1" :isShowTip="false" />
                <template v-else description="请上传身份证正面照片">
                  <imageUpload v-model="infoEditForm.cardPositive" modelKey="url" :limit="1" :isShowTip="false" />
                </template>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="身份证反面：" prop="cardBack">
                <imageUpload v-if="infoEditForm.cardBack" v-model="infoEditForm.cardBack" modelKey="url" :limit="1" :isShowTip="false" />
                <template v-else description="请上传身份证反面照片">
                  <imageUpload v-model="infoEditForm.cardBack" modelKey="url" :limit="1" :isShowTip="false" />
                </template>
              </el-form-item>
            </el-col>
          </template>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="infoEditVisible = false">关 闭</el-button>
          <el-button type="primary" :loading="subLoading" @click="infoEditSubmit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="infoEdit" lang="ts">
import api from '@/api/workMag/customerImport';
import { MemberInfoVo, InfoEditVo } from '@/api/workMag/customerImport/types';
import { checkRole } from '@/utils/permission'; // 权限判断函数

const emit = defineEmits(['getList'])
// 信息修改
const infoEditFormRef = ref<ElFormInstance>();
const infoEditVisible = ref(false);
const subLoading = ref(false);
const infoEditForm = ref<InfoEditVo>({
  id: '',
  cardPositive: '',
  cardBack: '',
  phone: '',
});


const rules = ref({
  phone: [{ required: true, message: '请输入手机号', trigger: 'change' }]
});




const infoDialogShowFun = async (row: MemberInfoVo) => {
  infoEditVisible.value = true;
  const res = await api.customerImporDetail(row.id);
  infoEditForm.value.id = res.data.id;
  infoEditForm.value.phone = res.data.phoneNoHide;
  infoEditForm.value.cardPositive = res.data.cardPositive;
  infoEditForm.value.cardBack = res.data.cardBack;
  infoEditForm.value.cardId = res.data.cardId;
  console.log(infoEditForm.value,'infoEditForm.value')
};
const infoEditSubmit = async () => {
  console.log(infoEditForm.value,'infoEditForm.value11')
  infoEditFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      subLoading.value = true;
      let params = infoEditForm.value;
      const res = await api.selUpUwInfoEditSubmit(params);
      console.log(res, 'res');
      if (res.code !== 200) {
        subLoading.value = false;
        ElMessage.error(res.msg || '修改失败，请稍后再试！');
        return;
      }
      infoEditVisible.value = false;
      ElMessage.success('修改成功！');
      subLoading.value = false;
      emit('getList',{});
    }
  });
};

defineExpose({
  infoDialogShowFun
});
</script>

<style lang="scss" scoped></style>
